import React, { useEffect, useState } from 'react'
import moment from 'moment'
export default function Timer() {
  let [newDate, setNewDate] = useState(moment().format('YYYY-MM-DD HH:mm:ss'))
  useEffect(() => {
    // 开启定时器
    console.log('定时器开启...')
    let timer = setInterval(() => {
      setNewDate(moment().format('YYYY-MM-DD HH:mm:ss'))
    }, 1000)
    return () => {
      // 关闭定时器
      console.log('定时器关闭...')
      clearInterval(timer)
    }
  }, [])
  return (
    <div>
      <p>当前时间为: {newDate}</p>
    </div>
  )
}
